package siga.visao_financeiro;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;


public class movTransfCaixa extends javax.swing.JDialog {
    public List combolist = new ArrayList();
    ImageIcon icone = new ImageIcon("/siga/imagem/Caixa_16x16.png");
    /** Creates new form movTransfCaixa */
    public movTransfCaixa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpTCPrincipal = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfCaixa1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jtfCaixa2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jtfValor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Transferência entre Contas");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jpTCPrincipal.setName("jpTCPrincipal"); // NOI18N

        jLabel1.setText("Transferir de:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCaixa1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Caixa Geral" }));
        jtfCaixa1.setName("jtfCaixa1"); // NOI18N

        jLabel2.setText("Transferir para:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfCaixa2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Caixa Geral" }));
        jtfCaixa2.setName("jtfCaixa2"); // NOI18N

        jLabel3.setText("Valor de Transferência:");
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setText("Transferir");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jtfValor.setName("jtfValor"); // NOI18N

        javax.swing.GroupLayout jpTCPrincipalLayout = new javax.swing.GroupLayout(jpTCPrincipal);
        jpTCPrincipal.setLayout(jpTCPrincipalLayout);
        jpTCPrincipalLayout.setHorizontalGroup(
            jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpTCPrincipalLayout.createSequentialGroup()
                .addGroup(jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpTCPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtfCaixa2, 0, 357, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpTCPrincipalLayout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpTCPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtfValor, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpTCPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfCaixa1, 0, 357, Short.MAX_VALUE)
                            .addGroup(jpTCPrincipalLayout.createSequentialGroup()
                                .addGroup(jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(158, 158, 158)))))
                .addContainerGap())
        );
        jpTCPrincipalLayout.setVerticalGroup(
            jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpTCPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfCaixa1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfCaixa2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jpTCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpTCPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpTCPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(jtfCaixa1.getSelectedIndex()!=jtfCaixa2.getSelectedIndex()){
            try{
                conexao.stm.executeUpdate("update fin_caixa set banco_vl_atual=banco_vl_atual-"+jtfValor.getText()+" where cod_emp="+index.cod_emp+" and codigo="+combolist.get(jtfCaixa1.getSelectedIndex()));
                conexao.stm.executeUpdate("update fin_caixa set banco_vl_atual=banco_vl_atual+"+jtfValor.getText()+" where cod_emp="+index.cod_emp+" and codigo="+combolist.get(jtfCaixa2.getSelectedIndex()));
                JOptionPane.showMessageDialog(null, "Transferência Efetuada.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                setConfirmaAcao(true);
                dispose();
            } catch (SQLException ex) {
                verErros.setErro("(Transferência):"+ex.getMessage());
            }
        }else{
            JOptionPane.showMessageDialog(null, "A selecção dos Caixas são iguais e não pode efetuar Transferência.","Alerta do Sistema",JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        setConfirmaAcao(true);
        movTransfCaixa.this.setVisible(false);
        movTransfCaixa.this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        try{
            conexao.Query("select fc.descricao, fc.codigo from usuarios_caixas uc inner join fin_caixa fc on fc.codigo=uc.cod_caixa and fc.cod_emp=uc.cod_emp where uc.cod_usuario="+index.usuario[0]+" and fc.cod_emp="+index.cod_emp+" order by fc.descricao");
            jtfCaixa1.removeAllItems();
            jtfCaixa2.removeAllItems();
            while(conexao.rs.next()){
                combolist.add(conexao.rs.getInt("codigo"));
                jtfCaixa1.addItem(conexao.rs.getString("descricao"));
                jtfCaixa2.addItem(conexao.rs.getString("descricao"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(movTransfCaixa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowActivated

     public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                movTransfCaixa.setConfirmaAcao(true);
            }
        });
    }

    protected static boolean ConfirmaAcao;
    
    public static boolean isConfirmaAcao() {
        return ConfirmaAcao;
    }

    public static void setConfirmaAcao(boolean ConfirmaAcao) {
        movTransfCaixa.ConfirmaAcao = ConfirmaAcao;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jpTCPrincipal;
    private javax.swing.JComboBox jtfCaixa1;
    private javax.swing.JComboBox jtfCaixa2;
    private javax.swing.JTextField jtfValor;
    // End of variables declaration//GEN-END:variables

}
